String.prototype.trim=function(){
	return this.replace(/^\s*|\s*$/g, "");
};
function login(lang){
	var url = location.href;
	var paraString = encodeURIComponent(url);
	var loc = WEBAPP+"login?from="+paraString;
    if(lang){
    	loc= loc+"&lang="+lang;
    }
    window.location =  loc;
	return false;
}

function HashMap(){
	/**Map大小**/
	var size=0;
	/**对象**/
	var entry=new Object();
	/**Map的存put方法**/
	this.put=function(key,value){
	if(!this.containsKey(key)){
	size++;
	entry[key]=value;
	}
	};
	/**Map取get方法**/
	this.get=function(key){
	    return this.containsKey(key) ? entry[key] : null;
	};
	/**Map删除remove方法**/
	this.remove=function(key){
	if(this.containsKey(key) && ( delete entry[key] )){
	size--;
	}
	};
	/**是否包含Key**/
	this.containsKey= function (key){
	    return (key in entry);
	};
	/**是否包含Value**/
	this.containsValue=function(value){
	for(var prop in entry)
	{
	if(entry[prop]==value){
	return true;
	}
	}
	return false;
	};
	/**所有的Value**/
	this.values=function(){
	var values=new Array();
	for(var prop in entry)
	{
	values.push(entry[prop]);
	}
	return values;
	};
	/**所有的 Key**/
	this.keys=function(){
	var keys=new Array();
	for(var prop in entry)
	{
	keys.push(prop);
	}
	return keys;
	};
	/**Map size**/
	this.size=function(){
	return size;
	};
	/**清空Map**/
	this.clear=function(){
	size=0;
	entry=new Object();
	};
} 

/*全局变量*/
var WEBAPP="/sview/";
var VESERV="/sview/viewEdit";
var ENTSERV="/sview/entity";
var DSSERV="/sview/dataset";
var URI_ENTITY_NO = 2;
var CUSTOM_ENTITY_NO = 1;
var COLLECTION_NO = 3;
var URI_DATASET_NO = 11;
var CUSTOM_DATASET_NO = 12;
var URI_ENTITY_STR = "o" ;
var CUSTOM_ENTITY_STR = "e";
var COLLECTION_STR = "c";
var URI_DATASET_STR = "u";
var CUSTOM_DATASET_STR = "c";
var MAKE_METHOD_RENAME=0;
var MAKE_METHOD_MERGE=1;
var MAKE_METHOD_CHAIN=2;
var MAKE_METHOD_CONNECT=3;
var URI_VTYPE =0;
var LABEL_VTYPE =1;
var CUSTOM_DMTYPE=0;
var ORIGIN_DMTYPE=1;
var TMP_DS_LOCATION ="/sview/dataset/t";

//need to be loaded from database
var RDF_TYPE_URI_ID = 478384;

var CUSTOM_QUERY_ITEM = 0;

//query item filter label
var FORWARD_EN = "forward";
var FORWARD_ZH = "前向属性";
var BACKWARD_EN = "backward";
var BACKWARD_ZH = "后向向属性";
var FORWARD_EN = "mygroup";
var FORWARD_ZH = "我的组";
var FORWARD_EN = "othergroups";
var FORWARD_ZH = "其它组";

var global_view_servlet;
var global_entity_servlet;
var global_history_servlet;
$(document).ready(function(){
	 global_view_servlet = WEBAPP+"views";
	 global_entity_servlet = WEBAPP+"entity";
	 global_history_servlet = WEBAPP+"historyServlet";
	  /*设置菜单*/
	  $(".account-box").live("mouseover",function(){
		  $(this).parent().addClass("ch");
	  });
	 
	  $(".account-box").live("mouseout",function(){
		  $(this).parent().removeClass("ch");
	  });
	  
	  $(".account-box").click(function(event){
		  hideAll($(this));
		  if($(this).find(".uinfo a").length==0){
		  	event.stopPropagation();
		  	$(".accmenu").toggle();
		  	$(this).parent().toggleClass("clk");
	       }
	  });
	 
	   $(".dmenu a").mouseover(function(){
		   $(this).parent().addClass("ah");
	   });
	   $(".dmenu a").mouseout(function(){
		   $(this).parent().removeClass("ah");
	   });
	   $(".entity-list li .sc").mouseover(function(){
		   $(this).addClass("ah");
	   });
	   $(".entity-list li .sc").mouseout(function(){
		   $(this).removeClass("ah");
	   });   
	   
	   //for history
	   $(".history-box").live("mouseover",function(){
			  $(this).parent().addClass("ch");
		  });
		 
		  $(".history-box").live("mouseout",function(){
			  $(this).parent().removeClass("ch");
		  });
		  
		  $(".history-box").click(function(event){
			  hideAll($(this));
//			  if($(this).find(".uinfo a").length==0){

			  	event.stopPropagation();
//			  	alert($(".historymenu").length);
			  	$(".historymenu").toggle();
			  	$(".history-box").parent().toggleClass("clk");
			  	
//			  var id = $(this).attr("id");
//			   var old = $(".active");
//			   if($(old).attr("id")==id){
//				   return;
//			   }else{
//				  $(old).removeClass("active");
//				  $(this).addClass("active");
//			   }
			   var lang = $(".history-box").attr("lang");
//			   var type = $(".history-box").attr("type"); 
			   var type = $(".historymenu .active").attr("id").replace("_tab","");
			   history(type,lang);
			   
		  });
		  
	   $(".historymenu .historynav.nav-tabs li").click(function(){
		   var id = $(this).attr("id");
		   var old = $(".historymenu").find(".active");
		   if($(old).attr("id")==id){
			   return;
		   }else{
			  $(old).removeClass("active");
			  $(this).addClass("active");
		   }
		   var lang = $(this).parent().attr("lang");
		   var type = id.replace("_tab","");
		   history(type, lang);
	   });
	   /***/
	
	   $("#set-option").click(function(event){
		   hideAll($(this));
		   var element = $(this).parent().parent();
		   var cclass ="cbgc";
		   
		   bgColor(element,cclass);
		   
		   event.stopPropagation();
		   $("#dmenu").toggle();
		   var button = $(this).find(".set_options");
		   button.toggleClass("npos");
	   });
	   
       document.onclick=function(e){ 
           var e=e?e:window.event;
           var tar = e.srcElement||e.target;
           if(tar.id!="set-option"){
        		hideGlobalSettingMenu();
           }
//           alert($(tar).hasClass("history-box")+"|"+$(tar).hasClass("historymenu")+"|"+$($(tar).parents(".historymenu")).length);
           if(!$(tar).hasClass("vset") && !$(tar).hasClass("rset")
        		   &&!$(tar).parent().hasClass("vset") && !$(tar).parent().hasClass("rset")){
        	   if(!$("#smenu").is(":hidden")){
        		   $("#smenu").hide();
        	   }
        	   $(".smenu").each(function(){
        		   if(!$(this).is(":hidden")){
            		   $(this).hide();
            	   }
        	   });
           }
           
           if(!$(tar).hasClass("custom_op")){
        	   if(!$("#emenu").is(":hidden")){
        		   $("#emenu").hide();
        	   }
           }
           
           if(!$(tar).hasClass("combo-arrow")){
        	   if(!$(".filter-box").is(":hidden")){
        		   $(".filter-box").hide();
        	   }
           }
           if(!$(tar).hasClass("account-box") && ($($(tar).parents(".accmenu")).length)<=0 ){
        	   if(!$(".accmenu").is(":hidden")){
        		   $(".accmenu").hide();
        		   $(".account-box").parent().removeClass("clk");
        	   }
           }
           //===for history
           if(!$(tar).hasClass("history-box") && !$(tar).hasClass("historymenu") && ($($(tar).parents(".historymenu")).length)<=0 ){
        	   if(!$(".historymenu").is(":hidden")){
        		   $(".historymenu").hide();
        		   $(".history-box").parent().removeClass("clk");
        	   }
           }
         //======
         };
     	$(".ftext").focus(function(){
    		$(this).css("border-color","#256EB1");
    	});
    	$(".ftext").blur(function(){
    		$(this).css("border-color","#BDC7D8");
    	});
    	
    	var browser = detect();
    	if(browser=="IE"){
    		var version = $.browser.version;
    		if(version<8){
    			alert("Sorry, IE "+version+" is not supported!");
    		}
    	}
    	
    	/*start:quick search widget*/
    	$('.myac-input').focus(function(){
    		$(this).addClass("focus");
    	});
    	
        $('.myac-input').blur(function(){
        	$(this).removeClass("focus");
    	});
        
       $(".mycomplete-item").live("mouseover",function(){
 		   $(this).addClass("bh");
 	   });
 	   
 	   $(".mycomplete-item").live("mouseout",function(){
 		   $(this).removeClass("bh");
 	   });
 	   
        /*end:quick search widget*/
 	   
 	   /*start:op hints*/
 	   $(".opHint .closeHint").live("click",function(){
 		   $(this).parent().remove()
 	   });
 	   /*end:op hints*/
});

function bgColor(e,cclass){
	if(!$(e).hasClass(cclass))
		$(e).addClass(cclass);
	else{
		$(e).removeClass(cclass);
	}
}


/**
 * 将e元素的html内容限定长度
 * @param e
 */
function checkContentLength(e,maxlength){
	if($(e).length>0){
	  var str = $(e).html();
	  if(str.length>maxlength){
		 var content = str.substring(0,maxlength) + "...";
		 $(e).html(content) ;
	  }
	}
}

function checkContentLengthWithMoreLess(e,maxlength){
  if($(e).length>0){
	var str = $(e).html();
	if(str.length>maxlength){
		var content = str.substring(0,maxlength) + "...";
		$(e).html(content) ;
		var more = $("<a class='more-content'>More</a>");
		$(e).append(more);
		var hidden = $("<div class='hidden'></div>").html(str);
		$(e).append(hidden);
	}
  }
}

/**
 * the div class is "edit_author"
 */
function createGroupSelectionDiv(userName,lang){
	var div =$('<div  class="edit_author" ></div>');
	if(lang=="en"){
		div.attr("title","Group Selection") ;
	}else if(lang=="zh"){
		div.attr("title","组选择");
	}
	var data={};
	data["user"] = userName;
    data =  JSON.stringify(data);
	 data=("group="+encodeURIComponent(data));
	 $.ajax({
		  url:global_view_servlet,
		  async:false,
		  data:data,
		  dataType:"json",
		  type:"post" ,
		  success:function(result){
			 if(result.length==0){
				 div.attr("t","n");
			 }else{
				 div.attr("t","y") ;
			 }
			 if(result.length>0){
				 var header=$(' <div class="header"></div>');
				 if(lang=="en"){
					 header.html("Which editing group you want to choose to do editing?");
				 }else if(lang=="zh"){
					 header.html("你选择哪个组进行编辑？"); 
				 }
				 div.append(header) ;
				 var select = $('<select class="gset"></select>');
				 div.append(select) ;
				 for(i=0;i<result.length;i++){
					 var element = result[i];
					 var groupID = element["gid"] ;
					 var groupName = element["gname"];
					 var option=$("<option></option>");
					 select.append(option) ;
					 option.attr("gid",groupID) ;
					 option.html(groupName);
				 }
//				 var svdft = $("<div class='hintdft'></div>");
//				 div.append(svdft);
//				 if(lang=="en"){
//					 svdft.html('<input type="checkbox" id="svdefault" checked />Use this editing group by default next time login');
//				 }
//				 else if(lang=="zh"){
//					 svdft.html('<input type="checkbox" id="svdefault" checked />下次登陆默认使用这个编辑组。');
//				 }
			 }else{
				 var header=$('<div class="header"></div>');
				 if(lang=="en"){
					 header.html("You don't belong to any editing group. You can create and manage " +
					 		"editing groups in 'My groups' panel. Go to 'My groups' now?");
				 }else if(lang=="zh"){
					 header.html("你尚未加入任何编辑组。你可以在'我的编辑组'中创建和管理组。现在去'我的编辑组'中吗？"); 
				 }
				 div.append(header) ;
			 }
		  }
    });
	 return div;
}

/**
 * 讲一段文本截断
 * @param text
 */
function cutText(text,maxlength){
	var result = text;
	if(result.length>maxlength){
		result = result.substring(0,maxlength) + "...";
	}
	return result;
}

function getSelectedGroup(){
	var result;
	result = $(".gset option:selected").attr("gid");
	return result;
}

function svDefault(){
	var result ="0";
	if($("#svdefault").is(":checked")){
		result ="1";
	}
	return result;
}


function setGroup(){
	var data = {};
	var selectedGid = getSelectedGroup();
	var svdefault  = svDefault();
	data["user"] = getUser();
	data["groupID"] = selectedGid;
	data["svdefault"] = svdefault;
	data = JSON.stringify(data);
	data = ("groupset=" + encodeURIComponent(data));
	$.ajax({
		url:"/sview/setting",
		async : false,
		data : data,
		dataType : "json",
		type : "post"
	});
}

/*detect browser and its version*/
function detect() { 

    if ($.browser.msie) 

        return "IE" ; 

    if ($.browser.mozilla) 

        return "Mozilla" ; 

    if ($.browser.safari) 

        return "Safari" ; 

    if ($.browser.opera) 

        return "Opera" ; 

 } 

function fetchLabel(container,uri_id,lang){
	 var data={};
	 data["uri_id"] = uri_id;
	 data["lang"] = lang;
    data =  JSON.stringify(data);
	 data=("label="+encodeURIComponent(data));
	 $.ajax({
		  url:global_entity_servlet,
		  async:true,
		  data:data,
		  dataType:"text",
		  type:"post" ,
		  success:function(result){
			  container.text(result);
		  }
    });
}

/*container is of ul.pvalue
 * 返回的每个结果的超链接是弹出新的页面
 * 
 * */
function fetchValues(displayLang,container,dmtype,dmid,inverse,genre,eid,status,flag){
	var meid = eid;
	var data = {};
	data["dmtype"] = dmtype;
	data["dmid"] = dmid;
	data["inverse"] = inverse;
	data["lang"] = displayLang;
	// which genre the eid belongs to
	data["genre"] = genre;
	data["eid"] = eid;
	data["status"] = status;
	data = JSON.stringify(data);
	data = ("viewDMValue=" + encodeURIComponent(data));
	var async = true;
	if (flag != "undefined") {
		async = flag;
	}
			$.ajax({
				url:global_view_servlet,
				async : async,
				data : data,
				dataType : "json",
				type : "post",
				success : function(result) {
					handleValues(result,container,displayLang);

					var status = result["status"];
					if (status != "stop") {
						setTimeout(function() {
							fetchValues(displayLang,container, dmtype, dmid, inverse,
									genre, meid, status, flag);
						}, 1000);
					} else {
						$(container).parent().parent().find(
									".titlebar .loading").hide();

						$(container).find("li:last").addClass("final");
					}
				}
			});
}

function handleValues(result,container,displayLang){
	/*
	 * result jsonArray format:
	 * [status:,values:[[etype],[eid],[valueLabel]]]
	 * etype:"l(literal)","o(origin uri)","e(custom
	 * entity)","c(custom collection)" eid: when etype=literal,
	 * eid is values hashcode valueLabel:literal lexical
	 * form,origin uri label, custom entity name ,may be an
	 * array
	 */	
	var values = result["values"];
	for (i = 0; i < values.length; i++) {
		var item = values[i];
		var etype = item["etype"];
		var eid = item["eid"];
		var euri = item["euri"];
		if ($(container).find(
				"li[etype=" + etype + "][eid=" + eid + "]").length > 0)
			continue;
		var li = $("<li></li>");
		$(container).append(li);
		li.attr("etype", etype);
		li.attr("eid", eid);
		var valueLabel = item["valueLabel"];
		if (etype == "l") {
			li.html(valueLabel);
		} else {
			if(item.directuri==1){
				var alink = $("<a class='vlink' target='_blank'></a>");
				alink.addClass("directlink");
				alink.text(euri);
				alink.attr("href", euri);
				alink.attr("title", euri);
				li.append(alink);
			}else if(item.directlabel==1){
				li.text(valueLabel);
			}else if(item.isimage==1){
				var img = $("<img/>");
				img.attr("src",euri);
				if(displayLang=="en"){
					img.attr("Loading Image");
				}else if(displayLang=="zh"){
					img.attr("加载图片");
				}
				li.append(img);
			}
			else{
				var alink = $("<a class='vlink' target='_blank'></a>");
				alink.text(valueLabel);
				alink.attr("href", getEntityLinkHref(etype, eid,displayLang));
				alink.attr("title", euri);
				li.append(alink);
				//对于uri，异步取label
				var needLabel = item["needLabel"];
				if(needLabel){
					fetchLabel(alink,eid,displayLang);
				}
			}
		}
	}
}

function handleValuesInBrowsing(result,container,displayLang){
	handleValues(result,container,displayLang);
	$(container).find("li a").removeAttr("target");
}

function scrollTop(element){
	$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body');
	$body.animate({scrollTop: $(element).offset().top}, 1000);
}

/*container is of ul.pvalue*/
function fetchValuesShowStatus(displayLang,container,dmtype,dmid,inverse,genre,eid,status,flag){
	var meid = eid;
	var data = {};
	data["dmtype"] = dmtype;
	data["dmid"] = dmid;
	data["inverse"] = inverse;
	data["lang"] = displayLang;
	// which genre the eid belongs to
	data["genre"] = genre;
	data["eid"] = eid;
	data["status"] = status;
	data = JSON.stringify(data);
	data = ("viewDMValue=" + encodeURIComponent(data));
	var async = true;
	if (flag != "undefined") {
		async = flag;
	}
			$.ajax({
				url:global_view_servlet,
				async : async,
				data : data,
				dataType : "json",
				type : "post",
				beforeSend:function(){
					container.addClass("loading2");
				},
				success : function(result) {
					handleValues(result,container,displayLang);

					var status = result["status"];
					if (status != "stop") {
						setTimeout(function() {
							fetchValuesShowStatus(displayLang,container, dmtype, dmid, inverse,
									genre, meid, status, flag);
						}, 1000);
					} else {
						container.removeClass("loading2");

						$(container).find("li:last").addClass("final");
					}
				}
			});
}


function getUser(){
	var user = null;
	if($(".uinfo a").length>0)
		user==null;
	else
		user = $(".uinfo").text().trim();
	return user;
}


function hideAll(item){
	  if(!$(item).hasClass("account-box")){
		  $(".accmenu").hide();
		  $(".account-box").parent().removeClass("clk");
	  }
	  if(!$(item).hasClass("history-box")){
		  $(".historymenu").hide();
		  $(".history-box").parent().removeClass("clk");
	  }
//	  alert(!$(item).children().hasClass("set_options"));
	  if(!$(item).children().hasClass("set_options")){
		  $("#dmenu").hide();
	  }
	  $("#smenu,.smenu").hide();
	  $("#emenu").hide();
	   var element = $("#set-option").parent().parent();
	   var cclass ="cbgc";
	   element.removeClass(cclass);
}
//=====for history 
function checkType(type){
	var flag = false;
	if(type=="entity"||type=="collection"||type=="dataset"){
		flag = true;
	}
	return flag;
}
function getChineseOfType(type){
	var ch = "";
	if(type=="entity"){
		ch = "实体";
	}else if(type=="collection"){
		ch = "实体集";
	}else if(type=="dataset"){
		ch = "数据集";
	}
	return ch;
}
function history(type,lang){
//	alert(type+"|"+lang);
	if(!checkType(type)){
		return;
	}
	$("#history-list").addClass("loading2");
	var data = {};
	data["user"] = getUser();
	data["type"] = type;
	data["num"] = 10;
	data["lang"] = lang; 
	data = JSON.stringify(data);
	data = ("getHistory="+encodeURIComponent(data));
	$.ajax({
		url: global_history_servlet,
		async: true,
		data: data,
		dataType: 'json',
		type: 'post',
		success: function(result){
			$("#history-list").removeClass("loading2");
			var container = $("#history-list");
			$(container).html("");
			var ul = $("<ul></ul>");
			$(container).append(ul);
			if(result==null || result["elements"]==null || result["elements"].length<=0){
				var infoText = "";
				if(result != null && result["status"]!=null && result["status"]!="ok"){
					infoText = result["status"];//====
					if(lang=="zh"){
						infoText = "非法的用户名或访问类型";
					}
				}else{
					if(lang=="en"){
						infoText="You haven't visited any "+type+" yet.";
					}else if(lang=="zh"){
						infoText="您最近没有浏览过任何"+getChineseOfType(type);
					}
				}
				var info = $("<li class='hinfo'><span></span></li>");
				$(info).text(infoText);
				$(container).append(info);
				return;
			}
			var array = result["elements"];
			for(var i=0; i<array.length; i++){
				var element = array[i];
				var genre = element["genre"];
				var id = element["id"];
				var name = element["name"];
				
				var a=$("<span class='link'></span>");
				var eid;
				var liclass = "";
				var spanclass = "";
				if(genre==COLLECTION_NO){
					href=ENTSERV+"/"+COLLECTION_STR;
					eid=COLLECTION_STR ;
					liclass = "lcollection"; 
					spanclass = "collection";
				} else if(genre==CUSTOM_ENTITY_NO){
					href =ENTSERV+"/"+CUSTOM_ENTITY_STR;
					eid=CUSTOM_ENTITY_STR ;
					liclass = "lentity"; 
					spanclass = "entity custom";
				} else if(genre==URI_ENTITY_NO){
					href=ENTSERV+"/"+URI_ENTITY_STR ;
					eid=URI_ENTITY_STR ;
					liclass = "lentity"; 
					spanclass = "entity uri";
				} else if(genre==URI_DATASET_NO){
					href =DSSERV+"/"+URI_DATASET_STR;
					eid = URI_DATASET_STR;
					liclass = "ldatawset"; 
					spanclass = "dataset uri";
				} else if(genre==CUSTOM_DATASET_NO){
					href =DSSERV+"/"+CUSTOM_DATASET_STR;
					eid = CUSTOM_DATASET_STR;
					liclass = "ldatawset"; 
					spanclass = "dataset custom";
				}
				href = href + id;
				if(lang == "zh"){
					href = href + "?lang=zh";
				}
				eid = eid + id;
				a.attr("loc",href);
				var name  = element["name"];
				   if($(ul).find("li[eid="+eid+"]").length>0){
					   if($(ul).find("li[eid="+eid+"]").find(".entity").html().trim()==name){
						   continue;
					   }
				   }
				   
				var li = $("<li></li>");
				li.attr("eid",eid);
				$(li).addClass(liclass);
				$(ul).append(li);    
				
				var div = $('<div class="sc split"></div>');
				li.append(div) ;
				div.append(a);
				var spanName = $("<span></span>");
				var spanType = $("<span class='type'></span>");
				if(name==""){
					spanName.addclass("vacant");
					if(lang=="en"){
						name = type+id;
					}else if(lang=="zh"){
						name = getChineseOfType(type)+id;
					}
				}
				spanName.text(name);
				spanName.attr("title",name);
				spanName.addClass(spanclass);
				a.append(spanName);
				var etype = element["type"];
				for(j=0; j<etype.length; j++){
					var span = $("<span></span>");
					spanType.append(span);
					var mtype  =etype[j] ;
					span.text(mtype["label"]);
					span.attr("type_id",mtype["type_id"]);
					if(j!=etype.length-1){
						var span = $("<span>, </span>");
						spanType.append(span);
					}   
				}
				div.append(spanType);	
			}//end of for
			   $(".history-list li .sc").mouseover(function(){
				   $(this).addClass("ah");
			   });
			   $(".history-list li .sc").mouseout(function(){
				   $(this).removeClass("ah");
			   });
			   $(".history-list li .sc").click(function(event){
				   event.stopPropagation();
				   window.location  = $(this).find(".link").attr("loc");
			   });
		}
	});
}

function showEntities(optype, start, length,lang,newFlag,user){
	 var data={};
	 data["type"] = optype;
	 data["start"] = start;
	 data["length"] = length;
	 data["lang"] = lang;
	 data["user"] = user;
    data =  JSON.stringify(data);
	 data=("entity="+encodeURIComponent(data));
	 $.ajax({
	  url:WEBAPP+"entity",
	  async:true,
	  data:data,
	  dataType:"json",
	  type:"post" ,
	  beforeSend:function(){
		  var element ;
		  if($(".entity-list").parent().find(".more-container").length>0)
			  element = $(".more-container");
		  else
			  element = $(".entity-list");
		  if(newFlag)
			  element =  $(".entity-list");
		  element.html("");
		  element.addClass("loading2");
	  },
	  success:function(result){
		  var ele ;
		  if($(".entity-list").parent().find(".more-container").length>0)
			  ele = $(".more-container");
		  else
			  ele = $(".entity-list");
		  if(newFlag)
			  ele =  $(".entity-list");
		  ele.removeClass("loading2");
		  var array = result["result"];
		  var hasMore = result["hasMore"];
		  var flag = false;
		  if(newFlag){
			  $(".entity-list").html("");
		  }
		  if($(".entity-list ol").length>0)
			  flag =true;
		  if(!flag){
			  var olt = $("<ol></ol>");
			  $(".entity-list").append(olt);
		  }
		  var ol  = $(".entity-list>ol") ;
		  for(i=0;i<array.length;i++){
			   var element = array[i];
			   var genre = element["genre"];
			   var id = element["id"];
			  
			   var a = $("<span class='link'></span>");
			   var href =WEBAPP+"entity/";
			   var eid ;
			   if(genre==COLLECTION_NO){
				   href=href+COLLECTION_STR;
				   eid=COLLECTION_STR ;
			   }
			   else if(genre==CUSTOM_ENTITY_NO){
				   href = href+CUSTOM_ENTITY_STR;
				   eid=CUSTOM_ENTITY_STR ;
			   }
			   else if(genre==URI_ENTITY_NO){
				   href= href+URI_ENTITY_STR ;
				   eid=URI_ENTITY_STR ;
			   }
			   href =href+id;
			   if(lang=="zh")
				   href = href+"?lang=zh" ;
			   eid= eid+id;
			   a.attr("loc",href) ;
			   var name  = element["name"];
			   if(ol.find("li[eid="+eid+"]").length>0){
				   if(ol.find("li[eid="+eid+"]").find(".entity").html().trim()==name){
					   continue;
				   }
			   }
			   
			   var li = $("<li></li>");
			   li.attr("eid",eid);
			   ol.append(li);
			 
			   if(genre==COLLECTION_NO){
				   li.addClass("lcollection");
			   }else
				   li.addClass("lentity");
			   
			   var div = $('<div class="sc split"></div>');
			   li.append(div) ;
			   
			   div.append(a) ;
			   var spanName = $("<span></span>") ;
			   var spanType = $("<span class='type'></span>");
			   if(name==""){
				   spanName.addClass("vacant");
				   if(lang=="en"){
					   name = "Entity"+id;
				   }else if(lang=="zh"){
					   name = "实体"+id;
				   }
			   }
			   spanName.text(name);
			   if(genre==COLLECTION_NO){
				   spanName.addClass("collection");
			   }else{
				   spanName.addClass("entity");
				   if(genre==CUSTOM_ENTITY_NO){
					   spanName.addClass("custom");
				   }else{
					   spanName.addClass("uri");
				   }
			   }
			   a.append(spanName);
			   var type = element["type"] ;
			   for(j=0;j<type.length;j++){
				   var span = $("<span></span>");
				   spanType.append(span);
				   var mtype  =type[j] ;
				   span.text(mtype["label"]);
				   span.attr("type_id",mtype["type_id"]);
				   if(j!=type.length-1){
					   var span = $("<span>, </span>");
					   spanType.append(span);
				   }
			   }
			   div.append(spanType);
		  }
		  $(".entity-list ol li:last>div").addClass("final");
		  $(".more-container").remove();
		  if(hasMore==true){
			  var div = $("<div class='more-container'></div>");
			  var moreDiv = $("<div class='moreDiv'></div>");
			  var a = null;
			  if(lang=='en')
				  a =  $('<span class="box"><span class="tip">Load More</span><span class="arrowDown"></span></span>');
			  else if(lang=='zh')
				  a =  $('<span class="box"><span class="tip">更多</span><span class="arrowDown"></span></span>'); 
			  moreDiv.append(a);
			  div.append(moreDiv);
			  a.parent().bind("click",function(){
				  $(".entity-list ol li>div.final").removeClass("final");
				  showEntities(optype, start+length, length,lang,false,user);
			  });
			  $(".entity-list").parent().append(div);
		  }
	  }
	});
}



//=====end of history====================
function logout(lang){
	var url = location.href;
	var paraString = encodeURIComponent(url);
	window.location = WEBAPP+"logout?lang="+lang+"&from="+paraString;
	return false;
}

function lang_set(lang){
	var url = location.href;
	var reg = /([\?&])lang=[^&]*&?/; 
	url = url.replace(reg, "$1");
	var last = url.charAt(url.length-1);
	var reg2 = /([\?&])$/;
	url = url.replace(reg2, "");
	var paraString = encodeURIComponent(url);
	if(lang=="en"){
	   window.location =WEBAPP+"preference?f="+paraString;
	}
	else if(lang=="zh"){
		window.location = WEBAPP+"preference?lang=zh&f="+paraString;
	}
	return false;
}

function group_set(lang){
	var url = location.href;
	var paraString = encodeURIComponent(url);
	if(lang=="en"){
	   window.location =WEBAPP+"groupset.jsp?f="+paraString;
	}
	else if(lang=="zh"){
		window.location = WEBAPP+"groupset_zh.jsp?f="+paraString;
	}
	return false;
}

function getEntityLinkHref(etype,eid,displayLang){
	var res = WEBAPP+"entity/"+etype+eid+"?lang="+displayLang;
	return res;
}

function hideGlobalSettingMenu(){
	if(!$("#dmenu").is(":hidden")){
	  $("#dmenu").hide();
	   var element = $("#set-option").parent().parent();
	   var cclass ="cbgc";
	   bgColor(element,cclass);
	   var button = $("#set-option").find(".set_options");
	   button.removeClass("npos") ;
	}
}